package com.ruoyi.module.version.protal;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.module.version.domain.SzVersionsUploadRecord;
import com.ruoyi.module.version.service.ISzVersionsUploadRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传记录Controller
 * 
 * @author ${author}
 * @date 2023-10-13
 */
@RestController
@RequestMapping("/protal/module/record")
public class SzVersionsUploadRecordController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(SzVersionsUploadRecordController.class);
    @Autowired
    private ISzVersionsUploadRecordService szVersionsUploadRecordService;

    /**
     * 查询文件上传记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SzVersionsUploadRecord szVersionsUploadRecord)
    {
        startPage();
        List<SzVersionsUploadRecord> list = szVersionsUploadRecordService.selectSzVersionsUploadRecordList(szVersionsUploadRecord);
        return getDataTable(list);
    }

    /**
     * 导出文件上传记录列表
     */
    @Log(title = "文件上传记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SzVersionsUploadRecord szVersionsUploadRecord)
    {
        List<SzVersionsUploadRecord> list = szVersionsUploadRecordService.selectSzVersionsUploadRecordList(szVersionsUploadRecord);
        ExcelUtil<SzVersionsUploadRecord> util = new ExcelUtil<SzVersionsUploadRecord>(SzVersionsUploadRecord.class);
        return util.exportExcel(list, "文件上传记录数据");
    }

    /**
     * 获取文件上传记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(szVersionsUploadRecordService.selectSzVersionsUploadRecordById(id));
    }

    /**
     * 新增文件上传记录
     */
    @Log(title = "文件上传记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzVersionsUploadRecord szVersionsUploadRecord)
    {
        return toAjax(szVersionsUploadRecordService.insertSzVersionsUploadRecord(szVersionsUploadRecord));
    }

    /**
     * 修改文件上传记录
     */
    @PreAuthorize("@ss.hasPermi('module:record:edit')")
    @Log(title = "文件上传记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzVersionsUploadRecord szVersionsUploadRecord)
    {
        return toAjax(szVersionsUploadRecordService.updateSzVersionsUploadRecord(szVersionsUploadRecord));
    }

    /**
     * 删除文件上传记录
     */
    @PreAuthorize("@ss.hasPermi('module:record:remove')")
    @Log(title = "文件上传记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szVersionsUploadRecordService.deleteSzVersionsUploadRecordByIds(ids));
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file)
    {
        try {
            return szVersionsUploadRecordService.uploadFile(file);
        } catch (Exception e) {
            LOGGER.error("文件上传失败",e);
            return AjaxResult.error("文件上传失败");
        }
    }

}
